
local player_manager = {}

local fds = {}

local players = {}

function player_manager.getPlayerByFd(from)
    local openid = fds[from]
    if openid then
        return players[openid]
    end
    return nil
end

function player_manager.getPlayerByOpenID(openid)
    return players[openid] or nil
end

function player_manager.removePlayer(openid)
    local player = players[openid]
    if player and player:getFd() then
        fds[player:getFd()] = nil
    end
    players[openid] = nil
end

function player_manager.addPlayer(player)
    players[player:getOpenID()] = player
end

function player_manager.onOnline(openid,from)
    local player = player_manager.getPlayerByOpenID(openid)
    if player then
        fds[from] = openid
        player:onOnline(from)
        return true
    end
    return false
end

function player_manager.onOffline(openid)
    local player = player_manager.getPlayerByOpenID(openid)
    if player then
        if player:getFd() then
            fds[player:getFd()] = nil
        end
        player:onOffline()
        return true
    end
    return false
end

return player_manager